//
//  TJSelectCityController.m
//  Teleport
//
//  Created by hc-101 on 2020/6/24.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJSelectCityController.h"


@interface TJSelectCityController ()

@property (strong, nonatomic) NSArray *keys;
@property (strong, nonatomic) NSDictionary *dataDict;

@end

static NSString * const selectCityCell = @"selectCityCell";
static NSString * const selectCitySectionView = @"selectCitySectionView";

@implementation TJSelectCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"选择国家地区".localizedString;
    
    self.navigationBar.leftBarButton = [TJButtonBarButton barButtonBackButtonTarget:self action:@selector(popViewController)];

    NSArray *citys = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
    NSArray *datas = [TJSelectCityModel mj_objectArrayWithKeyValuesArray:citys];
    
    NSMutableDictionary *addressBookDict = [NSMutableDictionary dictionary];
    
    [datas enumerateObjectsUsingBlock:^(TJSelectCityModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        
        
        NSString *firstLetterString =  [model.name getFirstLetterString];
        if (addressBookDict[firstLetterString]) {
            [addressBookDict[firstLetterString] addObject:model];
        } else {
            NSMutableArray *arrGroupNames = [NSMutableArray array];
            [arrGroupNames addObject:model];
            [addressBookDict setObject:arrGroupNames forKey:firstLetterString];
        }
    }];
    
    NSArray *nameKeys = [[addressBookDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
    
    // 将 "#" 排列在 A~Z 的后面
    if ([nameKeys.firstObject isEqualToString:@"#"]) {
        NSMutableArray *mutableNamekeys = [NSMutableArray arrayWithArray:nameKeys];
        [mutableNamekeys insertObject:nameKeys.firstObject atIndex:nameKeys.count];
        [mutableNamekeys removeObjectAtIndex:0];
        nameKeys = mutableNamekeys;
    }
    
    self.dataDict = addressBookDict.copy;
    self.keys = nameKeys.copy;
    
    
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 44;
    [self.tableView registerClass:UITableViewCell.self forCellReuseIdentifier:selectCityCell];
    [self.tableView registerClass:UITableViewHeaderFooterView.class forHeaderFooterViewReuseIdentifier:selectCitySectionView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
}
- (void)popViewController {
 [self dismissViewControllerAnimated:YES completion:nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _keys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *key = _keys[section];
    return [self.dataDict[key] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:selectCityCell forIndexPath:indexPath];
    NSString *key = _keys[indexPath.section];
    TJSelectCityModel *model = [self.dataDict[key] objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)", model.name, model.code];
    cell.textLabel.font = UIFont_Px(14);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.selectComplete) {
            NSString *key = self.keys[indexPath.section];
            TJSelectCityModel *model = [self.dataDict[key] objectAtIndex:indexPath.row];
            self.selectComplete(model);
        }
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (_keys.count) return 30;
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return _keys;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index {
    return index;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UITableViewHeaderFooterView *sectionView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:selectCitySectionView];
    NSString *sectionTitle = self.keys[section];
    sectionView.contentView.backgroundColor = UIColor.tj_backgroundColor;
    sectionView.textLabel.textColor = UIColor.blackColor;
    sectionView.textLabel.font = UIFont_Px(14);
    sectionView.textLabel.text = sectionTitle;
    return sectionView;
    
}


@end
